<?php

/**
 * Accesses the tgp_account_tokens table in the thegridpaper database
 * This table is the ManyToMany connection table between tgp_accounts and tgp_tokens
 * @author panosbaroudjian
 *
 */
class Application_Model_DbTable_AccountTokens extends Zend_Db_Table_Abstract
{
    protected $_name = 'tgp_account_tokens';
    protected $_primary = array('account_id', 'token_id');
    protected $_referenceMap = array(
                                    'Account' => array('columns' => array('account_id'),
                                                    'refTableClass' => 'Application_Model_DbTable_Accounts',
                                                    'refColumns' => array('account_id'),
                                                    'onDelete' => self::CASCADE),
                                    'Token' => array('columns' => array('token_id'),
                                                    'refTableClass' => 'Application_Model_DbTable_Tokens',
                                                    'refColumns' => array('token_id'),
                                                    'onDelete' => self::CASCADE));

    /**
     * Gets a row by its primary keys
     * @param int $account_id The id of the account
     * @param int $token_id The id of the token
     * @return NULL|array: An array containing the row if found, NULL otherwise.
     */
    public function getById($account_id, $token_id)
    {
        $account_id = (int)$account_id;
        $token_id = (int)$token_id;
        $row = $this->find($account_id, $token_id);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Gets a row with an accountId and a token
     * @param int $accountId The id of the account
     * @param string $token The token's value
     * @return NULL|array: An array containing the row if found, NULL otherwise.
     */
    public function getByToken($accountId, $token)
    {
        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
            ->setIntegrityCheck(false)
            ->joinUsing('tgp_tokens', 'token_id')
            ->where('tgp_tokens.value = ?', $token);
        $rows = $this->fetchAll($select);
        if (!$rows || $rows->count() == 0)
            return null;
        return $rows->current()->toArray();
    }

    /**
     * Inserts a new row in tgp_account_tokens
     * @param int $account_id The id of the account
     * @param int $token_id The id of the token
     */
    public function insertAccountToken($account_id, $token_id)
    {
        $data = array('account_id' => (int)$account_id, 'token_id' => (int)$token_id, 'pos_count' => 1);
        $this->insert($data);
    }

    /**
     * Updates the token counts
     * @param int $account_id The id of the account
     * @param int $token_id The id of the token
     * @param int $posCount The pos_count
     * @param int $negCount The neg_count
     */
    public function updateCounts($account_id, $token_id, $posCount, $negCount)
    {
        $where = array();
        $where['account_id = ?'] = (int)$account_id;
        $where['token_id = ?'] = (int)$token_id;
        $data = array('pos_count' => $posCount, 'neg_count' => $negCount);
        $this->update($data, $where);
    }

    /**
     * Deletes all the rows associated with the account's id
     * @param int $account_id The id of the account
     */
    public function deleteByAccount($account_id)
    {
        $where = $this->getAdapter()->quoteInto('account_id = ?', $account_id);
        $this->delete($where);
    }
}

